package com.xiaolei.library.Widgets;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.io.File;

/**
 * 对一些常用的场景，进行了自定义封装的WebView
 */
public class XWebView extends WebView
{
    public XWebView(Context context)
    {
        this(context, null);
    }

    public XWebView(Context context, AttributeSet attrs)
    {
        this(context, attrs, android.R.attr.webViewStyle);
    }

    public XWebView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context)
    {
        WebSettings settings = this.getSettings();
        // 启用Javascript
        settings.setJavaScriptEnabled(true);
        // 解决dom读取不到的问题
        settings.setDomStorageEnabled(true);
        // 解决https和http不能混用的问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        // 去掉过度拖动，导致的小阴影
        this.setOverScrollMode(View.OVER_SCROLL_NEVER);
        // 是否启用缓存
        settings.setAppCacheEnabled(true);
        // 是否允许加载本地资源
        settings.setAllowFileAccess(true);
        // 是否启用数据库
        settings.setDatabaseEnabled(true);
        File file = new File(context.getCacheDir(), "WebViewCache");
        if (!file.exists())
        {
            file.mkdirs();
        }
        // 设置缓存目录
        settings.setAppCachePath(file.getAbsolutePath());
        // 允许通过 file url 加载的 Javascript 读取其他的本地文件
        settings.setAllowFileAccessFromFileURLs(true);
        
        setWebChromeClient(new ChromeClient());
        setWebViewClient(new WebViewClient());
    }

    public static class ChromeClient extends WebChromeClient
    {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result)
        {
            result.confirm();
            return true;
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, JsResult result)
        {
            result.confirm();
            return true;
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result)
        {
            result.confirm();
            return true;
        }

        @Override
        public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result)
        {
            result.confirm();
            return true;
        }

        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage)
        {
            return true;
        }
    }

    public static class WebViewClient extends android.webkit.WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            if (url.startsWith("http") || url.startsWith("HTTP"))
            {
                view.loadUrl(url);
            }
            return true;
        }
    }
}
